#include "game_client.h"

LPDIRECT3D9 g_d3d = NULL;
HWND hWnd = NULL;
HINSTANCE hinstance = NULL;
extern void Setup_input();

void DestroyD3DWindow() {
	printf("Destroying D3D window...");

	if(g_d3d != NULL) g_d3d->Release();
	g_d3d = NULL;
}

LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) {
	switch(msg) {
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
		break;

	case WM_KEYUP:
		if(wp == VK_ESCAPE) PostQuitMessage(0);
		break;
	}

	return DefWindowProc(hWnd, msg, wp, lp);
}

bool init_window(int width, int height) {
	printf("Server init window...\n");
	Log::log("client_init::init_window(), width=%d, height=%d\n", width, height);
	WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0, 0, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, WINDOW_CLASS, NULL };
	RegisterClassEx(&wc);

	int screenWidth = GetSystemMetrics(SM_CXSCREEN);
	int screenHeight = GetSystemMetrics(SM_CYSCREEN);

	// Create the application's window
	hWnd = CreateWindow(WINDOW_CLASS, WINDOW_NAME, WS_OVERLAPPEDWINDOW, 0, 0, width, height, GetDesktopWindow(), NULL, wc.hInstance, NULL);
	hinstance = wc.hInstance;
	//Setup_input();
	return true;
}

HRESULT server_init() {
	printf("Init Direct3D in server...\n");

	if(g_d3d == NULL) {
		g_d3d = Direct3DCreate9(D3D_SDK_VERSION);
	}

	D3DDISPLAYMODE displayMode;
	if(FAILED(g_d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode))) return false;

	/////////////////////////////////////////////
	//read data
	int id = cc.read_int();
	UINT Adapter = cc.read_uint();
	D3DDEVTYPE DeviceType = (D3DDEVTYPE)(cc.read_uint());
	DWORD BehaviorFlags = cc.read_uint();
	D3DPRESENT_PARAMETERS d3dpp;
	cc.read_byte_arr((char*)(&d3dpp), sizeof(d3dpp));
	/////////////////////////////////////////////

	Log::log("server_init(), init_window start\n");

	//init_window(d3dpp.BackBufferWidth, d3dpp.BackBufferHeight);
	d3dpp.hDeviceWindow = hWnd;

	LPDIRECT3DDEVICE9 base_device = NULL;
	HRESULT hr = g_d3d->CreateDevice(Adapter, DeviceType, hWnd, BehaviorFlags, &d3dpp, &base_device);
	device_list.AddMember(id, base_device);

	switch(hr){
	case D3D_OK:
		Log::log("client_init(), create devie return D3D_OK\n");
		break;
	case D3DERR_DEVICELOST:
		Log::log("client_init(), create device return D3DERR_DEVICELOST.\n");
		break;
	case D3DERR_INVALIDCALL:
		Log::log("client_init(), create devie return D3DERR_INVALIDCALL\n");
		break;
	case D3DERR_NOTAVAILABLE:
		Log::log("client_init(), create device return D3DERR_NOTAVAILABLE.\n");
		break;
	case D3DERR_OUTOFVIDEOMEMORY:
		Log::log("client_init(), create device return D3DERR_OUTOFVIDEOMEMORY.\n");
		break;
	default:
		break;
			
	}
	if(base_device == NULL) {
		Log::log("client_init(), device is NULL, id=%d\n", id);
	}
	else {
		Log::log("client_init(), device is good, id=%d\n", id);
	}

	Log::log("server_init(), CreateDevice end\n");

	ShowWindow(hWnd, SW_SHOWNORMAL);
	UpdateWindow(hWnd);
	atexit(DestroyD3DWindow);

	return hr;
}


